﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// GoodsList.xaml 的交互逻辑
    /// </summary>
    public partial class ManagerList : Page
    {
        private DataTable dt = null;

        public ManagerList()
        {
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString() );
            InitializeComponent();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            query("");
            this.btnBar.ButtonDelete.IsEnabled = this.dataGrid.SelectedIndex == -1 ? false : true;
            this.btnBar.ButtonModify.IsEnabled = this.dataGrid.SelectedIndex == -1 ? false : true;
        }
        private void btn_query_Click(object sender, RoutedEventArgs e)
        {
            this.pagination.Reset();
            query(this.txt_name.Text);
        }
        private void query(string name)
        {
            string sql = "select t.id,t.name,t.sex,t.password,t.phone,t.type,t.permission,(case type when 1 then '普通管理员' else '超级管理员' end) as type_text from t_manager t where t.name like '%' + @name + '%'";
            dt = DBHelper.GetDataTable(sql, new Dictionary<string, object> { { "@name", name } }, "id", this.pagination.PagePer, this.pagination.Offset);
            dataGrid.ItemsSource = dt.DefaultView;
            int count = DBHelper.GetCount(sql, new Dictionary<string, object> { { "@name", name } });
            this.pagination.Init(count);
        }
        private void btn_refresh_Click()
        {
            query("");
        }
        private void btn_export_Click()
        {
            if (dt == null)
            {
                MessageBox.Show("没有需要导出的数据");
                return;
            }
            Dictionary<string, string> mapping = new Dictionary<string, string>();
            mapping.Add("name", "名称");
            mapping.Add("sex", "性别");
            mapping.Add("phone", "联系电话");
            mapping.Add("type", "类型");

            System.Windows.Forms.SaveFileDialog sd = new System.Windows.Forms.SaveFileDialog();
            sd.Filter = "Excel 2003 文档|*.xls";
            if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (File.Exists(sd.FileName))
                {
                    File.Delete(sd.FileName);
                }
                ExcelHelper.TableToExcel(dt, mapping, sd.FileName);
                if (MessageBox.Show("导出完毕，是否现在打开？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    System.Diagnostics.Process.Start(sd.FileName);
                }
            }
        }
        private void btn_delete_Click()
        {
            IList list = this.dataGrid.SelectedItems as System.Collections.IList;
            if (list == null || list.Count == 0)
            {
                MessageBox.Show("没有选中项");
                return;
            }

            if (MessageBox.Show("确定删除选中管理员吗？", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                foreach (var item in list)
                {
                    DataRow row = (item as DataRowView).Row;
                    int r = DBHelper.ExcuteSQL("delete t_manager where id = @id", new SqlParameter[] { new SqlParameter("id", row["id"]) }, CommandType.Text);
                }
                query("");
            }
        }
        private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.btnBar.ButtonDelete.IsEnabled = this.dataGrid.SelectedIndex == -1 ? false : true;
            this.btnBar.ButtonModify.IsEnabled = this.dataGrid.SelectedIndex == -1 ? false : true;
        }
        private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataRowView drv = this.dataGrid.SelectedItem as DataRowView;
            if (drv != null)
            {
                ManagerInput mi = new ManagerInput(Utils.ParseToDictionary(drv.Row));
                mi.Closed += Gi_Closed;
                mi.ShowDialog();
            }
        }
        private void Gi_Closed(object sender, EventArgs e)
        {
            query(this.txt_name.Text);
        }
        private void btn_add_Click()
        {
            ManagerInput mi = new ManagerInput();
            mi.Closed += Gi_Closed;
            mi.ShowDialog();
        }

        private void btn_modify_Click()
        {
            dataGrid_MouseDoubleClick(null, null);
        }
    }
}
